<?php 
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
MYDCONN();
	if(!isset($mode)) { $mode = 'ED'; }
	$TOT = '0.00';
	$_POST['CODE'] = 0;
	if(!isset($GET[1]['Ref'])){ $GET[1]['Ref'] = $_GET['Ref'];}
	if(!isset($GET[1]['TimeInvoice'])){ $GET[1]['TimeInvoice'] = $_GET['TimeInvoice'];}?>
<table width="98%"  border="0" cellpadding="2" cellspacing="1" bgcolor="#EAEAEA">
         <?php if ($mode == 'ED') { ?>
		  <tr><td>
            Natureza
            <?php mn_field('button',$mode,'changCODE','Selecionar','','','','" onclick="idwindow(\'Seleccione uma conta\',\'winccaccount\',\'\',\'Modules/CentrodeCustos/obj.php?FormTree=T\',600,410)'); 
			mn_field('text',$mode,'CODE',0,'6','6','',''); 
			mn_field('text',$mode,'CODEDESCRIPTION','','45','45','',''); ?>
              Fornecedor </td><td><div id="ProviderDiv"><?php include($GLOBALS['PAD'].'Modules/CentrodeCustos/Lib/sel_provider.php'); ?></div></td><td><div id="ContractDiv"></div></td><td>
              Loja <?php include($GLOBALS['PAD'].'Modules/CentrodeCustos/Lib/sel_office.php'); ?></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td colspan="3">Descrição</td>
            <td> Funcionário</td>
            <td>Quant.</td>
            <td>PreÇo</td>
            <td>Prom.</td>
            <td>IVA</td>
            <td>Total</td>
            <td rowspan="2" align="center">
			<?php mn_field('button',$mode,'petSubmit','','','','','insert" onclick="
			document.edtCdcInvoice.action=\'_response/rsproxy.php?Ref='.$GET[1]['Ref'].'&TimeInvoice='.$GET[1]['TimeInvoice'].'&pr=cdc_article\';
			subform(\'edtCdcInvoice\',\'invOtItems\');"'); ?></td>
          </tr>
          <tr bgcolor="#F8F8F8">
            <td colspan="3"><?php
			mn_field('hidden',$mode,'ArtTYP',0,'','','',''); 
			mn_field('text',$mode,'ArtDescription','','255','80','',''); 
			?></td>
            <td><div id="UserDiv"><?php include($GLOBALS['PAD'].'Modules/CentrodeCustos/Lib/sel_staff.php'); ?></div></td>
            <td><input name=ArtQuantity type="text" id=ArtQuantity23 value="1" size="2" maxlength="10" onKeyUp="ArticleAmount(this.form);"></td>
            <td><input name=ArtUAmount type="text" id=ArtUAmount23 value="0.00" size="4" maxlength="14" onKeyUp="ArticleAmount(this.form);"></td>
            <td><input name=ArtPromo type="text" id=ArtPromo23 value="0" size="4" maxlength="6" onKeyUp="ArticleAmount(this.form);"></td>
            <td><?php
            mn_field('select',$mode,'ArtTaxPercent','',"SELECT PERCENT AS ID, PERCENT AS NAME FROM ccenter_gov_tax WHERE Status = 1",'','','" onchange="ArticleAmount(this.form);'); ?></td>
            <td><input name=ArtAmount type="text" readonly="" id=ArtAmount4 value="0.00" size="4" maxlength="20"></td>
          </tr>
		  <?php } ?>
    <?php
	$Offices = $GLOBALS['MYD']->db_array("SELECT 
					main_offices.name,
					a.Office,
					SUM(a.Amount) AS Amount
	  			FROM main_offices
				LEFT OUTER JOIN ccenter_invarticles AS a ON main_offices.ref = a.Office
				WHERE RefInvoice = {$GET[1]['Ref']}
				AND a.Status = 1
				GROUP BY Office
				ORDER BY Office");
	$Plaf = $GLOBALS['MYD']->db_array("SELECT 
					Office, SUM(ccenter_petty.Plafond) AS Plaf
	  			FROM ccenter_petty
				WHERE sysTime > {$GET[1]['TimeInvoice']}
				GROUP BY Office
				ORDER BY sysTime DESC"); 
$List = $GLOBALS['MYD']->db_array("SELECT 
		a.Office AS OfficeID,
		main_users.name AS User,
		main_offices.name AS Office,
		a.ArtReference,
		a.Ref,
		a.CODE,
		a.Provider,
		a.ArtReference,
		a.Description,
		a.Quantity,
		a.UAmount,
		a.IvaPercent,
		a.Promo,
		a.Amount,
		DATE(a.sysTime) AS sysTime,
		ccenter_providers.name AS Provider,
		ccenter_contas.name AS CODEX
	FROM ccenter_invarticles AS a
	LEFT OUTER JOIN main_users ON main_users.ref = a.User
	LEFT OUTER JOIN main_offices ON main_offices.ref = a.Office
	LEFT OUTER JOIN ccenter_providers ON ccenter_providers.Ref = a.Provider
	LEFT OUTER JOIN ccenter_contas ON ccenter_contas.CODE = a.CODE
	WHERE a.RefInvoice = {$GET[1]['Ref']}
	AND a.Status = 1
	GROUP BY a.Ref
	ORDER BY a.Office,a.Ref");
$LastPlaf = 0;
$it = 1;
# LISTA LOJAS			
for ($o = 1; $o <= $Offices[0]; $o++) {
	$TOT += $Offices[$o]['Amount'];
	?>
 <tr bgcolor="#F8F8F8">
        <td colspan="13" bgcolor="#DBDBDB">Loja
		<?php mn_field('text',$mode,'',$Offices[$o]['name'],'25','25','','" readonly="'); ?>
		Total: EUR
		<?php mn_field('text',$mode,'Amount',$Offices[$o]['Amount'],'19','10','','" readonly="'); ?>
		Limite: EUR
        <?php mn_field('text',$mode,'Plaf-'.$Offices[$o]['Office'],$Plaf[$o]['Plaf'],'19','10','','" readonly="'); 
		mn_field('button',$mode,'InsertPlaf',$Offices[$o]['Office'],'','','',''); ?>
		</td>
        </tr>
           <tr bgcolor="#F8F8F8">
            <td colspan="3">Descrição</td>
            <td bgcolor="#F8F8F8">Natureza</td>
            <td>Fornecedor</td>
            <td>Funcionário</td>
            <td NOWRAP>Data</td>
            <td>Quant.</td>
            <td>PreÇo</td>
            <td>Prom.</td>
            <td>IVA</td>
            <td>Total</td>
            <td></td>
          </tr>
<?php
	# LISTA ITEMS DE LOJAS
	for($r = $it; $r <= $List[0]; $r++) {
		if ($Offices[$o]['Office'] == $List[$r]['OfficeID']){
		echo '<tr bgcolor="#B0C0D0">
			<td colspan="3" width="280">'.$List[$r]['Description'].'</td>
            <td>';
			mn_field('text',$mode,'Chan'.$r,$List[$r]['CODE'],'','6','6','');
			mn_field('button',$mode,'Edt'.$r,'Mudar','','6','6','" onclick="
				document.edtCdcInvoice.action=\'_response/rsproxy.php?Ref='.$GET[1]['Ref'].'&CODE=\'+document.edtCdcInvoice.Chan'.$r.'.value+\'&ACT=ED&RCINV='.$List[$r]['Ref'].'&TimeInvoice='.$GET[1]['TimeInvoice'].'&pr=cdc_article\';
        		subform(\'edtCdcInvoice\',\'invOtItems\');'); echo '</td>
	    	<td><div id="ProviderDiv">'; 
	    	include($GLOBALS['PAD']."Modules/CentrodeCustos/Lib/sel_provider.php"); echo '</div></td>
	    	<td>'.$List[$r]['User'].'</td>
	        <td>'.$List[$r]['sysTime'].'</td>
	        <td align="right">'.$List[$r]['Quantity'].'</td>
	        <td align="right">'.$List[$r]['UAmount'].'</td>
	        <td>'.$List[$r]['Promo'].'</td>
	        <td>'.round($List[$r]['IvaPercent']).'%</td>
	        <td align="right">'.$List[$r]['Amount'].'</td>
			<td>'; mn_field('button',$mode,'delArticle','','','','','delete" onclick="
				document.edtCdcInvoice.action=\'_response/rsproxy.php?Ref='.$GET[1]['Ref'].'&ACT=RT&RCINV='.$List[$r]['Ref'].'&TimeInvoice='.$GET[1]['TimeInvoice'].'&pr=cdc_article\';
        		subform(\'edtCdcInvoice\',\'invOtItems\');'); echo '</td>
		</tr>';
		} elseif($r > 1) {
		$it = $r;
		$r = $List[0];}
		
	}
}?>
<tr align="right" bgcolor="#F2F2F2">
    <td colspan="12">
	<b>Total: &euro;</b>
	<?php mn_field('text',$mode,'Amount',$TOT,'20','14','','INPNUM'); ?>
	</td>
    </tr>
    </table>